package com.weilan.blog.admin.controller;

import com.weilan.blog.admin.service.BlogService;
import com.weilan.blog.admin.vo.BlogVO;
import com.weilan.blog.base.exception.ThrowableUtils;
import com.weilan.blog.base.validator.group.Delete;
import com.weilan.blog.base.validator.group.GetList;
import com.weilan.blog.base.validator.group.Insert;
import com.weilan.blog.base.validator.group.Update;
import com.weilan.blog.common.annotion.AvoidRepeatableCommit.AvoidRepeatableCommit;
import com.weilan.blog.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * @description: 博客相关接口
 * @author: 孙 峰
 * @time: 2020/10/20 14:02
 */
@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogService blogService;

    /**
     * 获取博客列表
     *
     * @param blogVO
     * @param result
     * @return
     */
    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody BlogVO blogVO, BindingResult result) {

        ThrowableUtils.checkParamArgument(result);
        return ResultUtil.successWithData(blogService.getPageList(blogVO));
    }

    /**
     * 增加博客
     *
     * @param blogVO
     * @param result
     * @return
     */
    // 重复提交注解
    @AvoidRepeatableCommit
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody BlogVO blogVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return blogService.addBlog(blogVO);
    }

    /**
     * 本地博客上传
     *
     * @param filedatas
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadLocalBlog")
    public String uploadPics(@RequestBody List<MultipartFile> filedatas) throws IOException {

        return blogService.uploadLocalBlog(filedatas);
    }

    /**
     * 编辑博客
     *
     * @param blogVO
     * @param result
     * @return
     */
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody BlogVO blogVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return blogService.editBlog(blogVO);
    }

    /**
     * 推荐博客排序调整
     *
     * @param blogVOList
     * @return
     */
    @PostMapping("/editBatch")
    public String editBatch(@RequestBody List<BlogVO> blogVOList) {
        return blogService.editBatch(blogVOList);
    }

    /**
     * 删除博客  删除一个
     * @param blogVO
     * @param result
     * @return
     */
    @PostMapping("/delete")
    public String delete(@Validated({Delete.class}) @RequestBody BlogVO blogVO, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return blogService.deleteBlog(blogVO);
    }

    /**
     * 删除博客批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteBatch/{ids}")
    public String deleteBatch( @PathVariable("ids") Set<String> ids) {
        System.out.println("aaaaaaa");
        return blogService.deleteBatchBlog(ids);
    }


}
